package com.example.com.daervideo.popuwindow;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.design.widget.BottomSheetBehavior;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.com.daervideo.R;
import com.example.com.daervideo.activitys.MainActivity;

import static android.support.design.widget.BottomSheetBehavior.STATE_EXPANDED;

/**
 * Created by i7-1 on 2018/12/8.
 */

public class MessagePpWindow extends PopupWindow {

    private final BottomSheetBehavior<RelativeLayout> behavior;

    public MessagePpWindow(Context context) {
        final View mMenuView = LayoutInflater.from(context).inflate(R.layout.layout_bottom_message, null);


        RelativeLayout bottomSheet = mMenuView.findViewById(R.id.bottom_sheet);
        behavior = BottomSheetBehavior.from(bottomSheet);

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //适配虚拟键
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x30000000); //android:background="#30000000"
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
              //  dismiss();
               /* int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }*/
                return true;
            }
        });
    }

    public void show(MainActivity mainActivity, int position) {
        behavior.setState(STATE_EXPANDED);
        showAtLocation(mainActivity.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);

    }
}
